Repository navigation
Fix PyCall::PythonNotFound on Windows - #236
Conversation
Python was not found or failed to load on Windows for three reasons: - Python that is not on PATH was never found. Fall back to python.exe in CONDA_PREFIX, and then to the PEP 514 entries in the registry under Software\Python, which includes ContinuumAnalytics for Anaconda. An explicitly given python is still used alone. - sysconfig has no SHLIB_SUFFIX on Windows, which raised TypeError while building the candidate names. Default it to .dll and add the python311-style name. - Fiddle.dlopen of the right python3xx.dll failed with "No such file or directory". The slashes are not the cause: backslashes fail the same way and forward slashes work once the dependencies resolve. The cause is that dependencies like zlib.dll in the folder of the DLL are not found, and RubyInstaller excludes PATH from the DLL search. Register the folder of the DLL and Library\bin with RubyInstaller::Runtime.add_dll_directory, or prepend them to PATH on other Ruby distributions. Also stop printing the complaint of the Microsoft Store python.exe alias on every start, unless PYCALL_DEBUG_FIND_LIBPYTHON is set. Fixes red-data-tools#86 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
String#delete_suffix requires Ruby 2.5, so candidate_names raised NoMethodError on Ruby 2.4 when python_config has LIBRARY. It was not noticed because candidate_names only runs when the first candidate fails to load, until the new finder spec reached it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@felixmarch Thank you for the patch! The Windows-specific logic is making the existing Before merging, could you move the Windows-specific implementation into a return FinderForWindows.find_libpython(python) if windows?This would keep the existing flow clear and the Windows implementation separate. |
As requested in the review, Finder.find_libpython now delegates to FinderForWindows.find_libpython at its beginning on Windows, and Finder is back to its previous flow. FinderForWindows keeps the Windows behavior as it was: finding Python in CONDA_PREFIX and the registry, the .dll suffix and python311.dll-style names, and registering the DLL folders before Fiddle.dlopen. It reuses Finder.normalize_path, Finder.make_libpaths and the investigator script. It now raises PythonNotFound when no library can be loaded, instead of returning the result of the last candidate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Okay, Windows implementation is moved into On Windows, |
| module LibPython | ||
| # Everything except the real registry access runs on all platforms, so that | ||
| # the Windows behavior is also covered by the CI on Linux and macOS. | ||
| ::RSpec.describe FinderForWindows do |
There was a problem hiding this comment.
@felixmarch Could you please put the if: Fiddle::WINDOWS condition on this context instead of the context of Windows registry things?
There was a problem hiding this comment.
Done in 6d301f1: the if: Fiddle::WINDOWS condition is now on the top-level describe FinderForWindows, and I removed it from the registry specs.
|
@felixmarch Could you clarify which Python distributions and installation setups are actually tested by the Windows jobs in GitHub Actions? I see jobs using actions/setup-python and Miniconda, with PYTHON=python set in both cases. Are configurations with PYTHON unset, Python outside PATH, or discovery through CONDA_PREFIX or the PEP 514 registry also tested with real Python installations? I’d like to understand which cases are covered by end-to-end CI tests and which are covered only by unit tests or manual testing. |
…238) Fixes #87. ## Problem `PyCall.import_module('scipy.stats').norm.pdf([1, 2, 3], 0, 10)` raises ``` TypeError: rv_continuous_frozen.pdf() takes 2 positional arguments but 4 were given ``` while the same expression works in Python. `scipy.stats.norm` is not a class or a function but a callable *instance* of `norm_gen`. Ruby cannot distinguish `stats.norm` from `stats.norm()`, and `pycall_pyobject_wrapper_wrapper_method` calls every callable attribute, so `stats.norm.pdf(x, 0, 10)` is evaluated as `stats.norm().pdf(x, 0, 10)`: `pdf` of a frozen distribution (loc=0, scale=1), which only takes `x`. ## Change When an attribute is accessed **without arguments**, it is called only if it is a *routine*. Otherwise the callable object is returned as is. **Calls with arguments are unchanged.** "Routine" follows Python's own definition in `inspect.isroutine()`, implemented in C with type checks (no Python-level call per access): - `builtin_function_or_method`, bound `method`, and `method-wrapper` (and subtypes) - non-data descriptors: types that define `__get__` but not `__set__`. This covers Python functions, method descriptors, slot wrappers, Cython functions (`cython_function_or_method`), `functools.lru_cache` wrappers, numpy's `_ArrayFunctionDispatcher` (`np.sum`, etc.), and so on. Two additions to keep existing code working: - `functools.partial`: it is a routine on Python 3.14+ (it gained `__get__`), and it is special-cased so that the behavior does not depend on the Python version. - Ruby `Proc`/`Method` objects stored in a Python attribute are still called, as before. Class objects are still returned as is, as before. ## Behavior change With no arguments, these are now **returned** instead of being called: | Attribute | Before | After | |---|---|---| | `scipy.stats.norm` and other rv instances | called: frozen distribution | the generator object | | numpy ufuncs (`np.sin`, `scipy.special.gamma`) | called with no args: `TypeError` | the ufunc object | | other instances of classes defining `__call__`, `operator.itemgetter(...)` | called | the object | | a non-callable Ruby object stored in a Python attribute | `TypeError: non-callable ruby object` | the Ruby object | Everything else is unchanged. I checked these against real numpy/scipy: `np.get_include`, `np.random.random`, `np.random.default_rng` (Cython), `ndarray.sum`/`copy`/`tolist`, staticmethods and classmethods are all still called without arguments. **Risk:** code that relies on `obj.callable_instance` calling it with zero arguments now gets the object back. It can use `obj.callable_instance.()` (or `.call`) instead. The README section "The callable attribute of an object" now documents this. ## Tests - New `spec/pycall/callable_attribute_spec.rb` with a scipy-like helper module `spec/python/pycall/callable_attribute.py` (no scipy needed). It covers the callable-instance case (no args, positional args, keyword args, `.()`, class/instance attributes). It also checks that zero-argument functions, bound methods, staticmethods, classmethods, builtin methods and functions, method-wrappers, `lru_cache` wrappers, `partial` objects, callable non-data descriptors, and Ruby lambdas are still called. - Without the C change, the 4 callable-instance specs fail. With it, the full suite passes: 212 examples, 0 failures, 27 pending. - Tested on Windows Server 2025, RubyInstaller Ruby 3.4 (UCRT), Anaconda Python 3.14.6, numpy 2.x and scipy 1.18. To load Python there, I used a local workaround for the Windows finder (the issue addressed by #236); it is not part of this PR. ## Not included - `pyfrom ..., import: :norm` (`lib/pycall/import.rb`) still defines a method that calls any non-class callable, so the same issue remains there. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@mrkn I did not change workflow files but tested it manually like this: My current python is from Anaconda3-2026.07-1-Windows-x86_64, which is python 3.14.6 This issue has existed for long time, so I believe it happens to any version of anaconda in Windows (see my early report two years ago on issue #86). |
As requested in the review, put the `if: Fiddle::WINDOWS` condition on the whole FinderForWindows spec instead of only on the registry specs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@felixmarch Thank you for clarifying. I think the behavior introduced by this PR needs to be tested against actual Python installations in CI running on Windows, rather than relying only on mocked tests. Could you also update the Windows GitHub Actions workflow to cover the new discovery paths with real Python installations? This would let us verify that Python discovery and DLL loading work together in the environments this PR is intended to support. |
As requested in the review, add a discovery job to the Windows workflow that uses real Python installations instead of mocks. Each job hides every python.exe on PATH and checks that PyCall loads the expected Python, imports modules that need DLLs, and passes the test suites: - registry-pythoncore: Python registered under PythonCore - registry-anaconda: Miniconda installed with /RegisterPython=1, the only Python registered (under ContinuumAnalytics) - conda-prefix: a conda environment given by CONDA_PREFIX finder_spec.rb is skipped there because it runs python on PATH. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The registry jobs found the expected Python, but two specs failed because numpy was not installed there. Install numpy in both of them. The runner image registers every Python in its tool cache under PythonCore, so the registry-pythoncore job now removes all of them except the Python of setup-python, and expects exactly that one. Miniconda installed with /RegisterPython=1 registers itself under PythonCore, not under ContinuumAnalytics, so rename registry-anaconda to registry-miniconda and correct the comment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@kou Could you please review the window finder and the workflow changes? |
|
@mrkn I added a
Each job does the following:
All three pass: https://github.com/felixmarch/pycall.rb/actions/runs/43131380225 One gap may remain. The current Miniconda installer registers itself under Can you please provide opinion on Anaconda's terms? The Miniconda installer is BSD-licensed, and Anaconda's payment terms apply to using its package repositories. The job would only download the installer and install numpy with pip. Still, the terms don't say explicitly whether downloading an installer from repo.anaconda.com in CI is fine for an open-source project. The |
kou
left a comment
There was a problem hiding this comment.
I think that we can use repo.anaconda.com to download an installer.
Or we can use Miniforge instead of Minoconda.
Can we simplify specs? I think that most checks are covered by Windows CI jobs.
| end | ||
|
|
||
| def debug? | ||
| @debug ||= (ENV['PYCALL_DEBUG_FIND_LIBPYTHON'] == '1') |
There was a problem hiding this comment.
This result isn't cached with non '1' value. How about removing @debug ||=?
If we want to cache it, we can use @debug_env ||= ENV['PYCALL_DEBUG_FIND_LIBPYTHON'] || ''; @debug_env == '1'.
There was a problem hiding this comment.
Removed @debug ||= in ba6f707, so the environment variable is read each time.
|
|
||
| def add_dll_directory(path) | ||
| debug_report("add_dll_directory(#{path.inspect})") | ||
| if defined?(RubyInstaller::Runtime) && RubyInstaller::Runtime.respond_to?(:add_dll_directory) |
There was a problem hiding this comment.
Do we need the RubyInstaller::Runtime.respond_to?(:add_dll_directory) check?
| if defined?(RubyInstaller::Runtime) && RubyInstaller::Runtime.respond_to?(:add_dll_directory) | |
| if defined?(RubyInstaller::Runtime) |
There was a problem hiding this comment.
Applied in ba6f707. RubyInstaller::Runtime.add_dll_directory exists since RubyInstaller 2.4.1-1, the first RubyInstaller2 release, so the check is not needed.
| # implementation like the py launcher does, then Anaconda. | ||
| PYTHON_REGISTRY_COMPANIES = [ | ||
| -'PythonCore', | ||
| -'ContinuumAnalytics', |
There was a problem hiding this comment.
'ContinuumAnalytics' is for old Miniconda, right? Can we remove this?
There was a problem hiding this comment.
Yes. Old installers like Miniconda3-4.5.12 (January 2019) contain Software\Python\ContinuumAnalytics, while the current ones register under PythonCore. I removed it in ba6f707, so only PythonCore is preferred now. The entries of every company are still read, so an old installation registered under ContinuumAnalytics is still found, after PythonCore.
- Prefer only PythonCore in the registry. ContinuumAnalytics was written by old Anaconda installers; such entries are still found, after PythonCore, like those of any other company. - Call RubyInstaller::Runtime.add_dll_directory whenever RubyInstaller::Runtime is defined, which has it since RubyInstaller 2.4.1-1. - Read PYCALL_DEBUG_FIND_LIBPYTHON each time, since @debug ||= did not cache a false result anyway. - Install Miniforge instead of Miniconda in the discovery jobs, so that they use neither Anaconda's installers nor its default channels. - Keep only the specs for what the discovery jobs cannot cover: an explicitly given python, the order and the architecture of the registry entries, and loading without RubyInstaller. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@kou I switched the discovery jobs to Miniforge in ba6f707: I also simplified the specs. All the Windows jobs pass, including the three discovery jobs: https://github.com/felixmarch/pycall.rb/actions/runs/43156807845 |
|
Thanks. Could you check comments from Claude Code? Can we move
We don't need to run the full test suite again in these steps because the previous Could you also update the existing
|
As suggested in the review, simplify the workflow: - The test job now also finds Python in the registry after hiding every python.exe on PATH. The runner image registers the Pythons of its tool cache under PythonCore, and the newest one is used, so only standard libraries are imported there. - The conda job now also finds Python in CONDA_PREFIX, which setup-miniconda sets, the same way, and imports numpy too. - The conda job no longer continues on error. Its steps failed on master because SHLIB_SUFFIX is None in conda's Python, and pass now. - Remove the "Add conda's DLL path" step. It wrote to $GITHUB_PATH instead of $env:GITHUB_PATH, so it never changed PATH, and PyCall adds the DLL directories by itself now. - The conda job installs Miniforge instead of using the defaults channel. - Only Miniforge registered by its installer is left in its own job, which still runs the suites with the Python that PyCall finds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With Miniforge, bundle install failed to build fiddle because the compiler of MSYS2 failed while the conda-forge environment came first in PATH. Build the gems and the extension before setup-miniconda, as the former conda-prefix job did. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rake runs extconf.rb again, because the Makefile depends on its directory, which the build changes after writing the Makefile. The compiler then fails while the conda-forge environment is activated, so run the test and spec suites directly, as the registry job does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@kou I've done these in 6f6e159, 3099185 and cfd2e9f.
I also confirmed your points in the CI logs. On master, the All the Windows jobs pass: https://github.com/felixmarch/pycall.rb/actions/runs/43180929779 Glad to know we're using the same AI tool here. Yes, I'm using Claude Code for all the coding in this PR. |
Fixes #86
Problem
On Windows, PyCall failed to find or load Python for three separate reasons:
sysconfighas noSHLIB_SUFFIXon Windows, so building the candidate names raisedTypeError.Fiddle.dlopenof the correctpython3xx.dllfailed with "No such file or directory".The third error is misleading. It looked like a forward slash vs. backslash problem, but it isn't: backslashes fail the same way, and forward slashes work once the dependencies resolve. The actual cause is that the DLL's dependencies, such as
zlib.dllin the same folder, aren't found. Adding the folder to PATH doesn't help on RubyInstaller, which excludes PATH from the DLL search.Changes
All code changes are in
lib/pycall/libpython/finder.rb:python3andpython, trypython.exeinCONDA_PREFIX, then the PEP 514 registry entries underSoftware\Python, includingContinuumAnalytics. Entries for another architecture are skipped. An explicitly givenPYTHONis still used alone.SHLIB_SUFFIXto.dlland add thepython311.dll-style name.Library\binwithRubyInstaller::Runtime.add_dll_directory, or prepend them to PATH on other Ruby distributions.python.exealias unlessPYCALL_DEBUG_FIND_LIBPYTHONis set.String#chompinstead ofString#delete_suffix, which requires Ruby 2.5. The new specs reach this line.The README also describes the new search order on Windows.
Testing
Tested on Windows Server 2025 with RubyInstaller 3.4 (UCRT) and Anaconda (Python 3.14):
rake spec: 220 examples, 0 failures, 27 pendingPYTHONset, and with onlyCONDA_PREFIXset, including importingssl,ctypes,sqlite3,numpyandpandasHKCUkeyThe CI and Windows workflows pass on the fork.
Limitations
CONDA_PREFIXand noPYTHONstill raisesPythonNotFound.Software\Pythonkey was not exercised end to end, because the test machine has no such entry. The lookup is covered by specs against a temporary key.The registry specs create and delete a temporary key under
HKCUwhen run on Windows.🤖 Generated with Claude Code